package fengxici.demo.activemq.spring.controller;

import fengxici.demo.activemq.spring.service.LockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Fengxici on 2018/1/31.
 */
@Controller
public class LockController {
    @Autowired
    LockService lockService;

    @RequestMapping(value = "/lock", method = RequestMethod.GET)
    @ResponseBody
    public void lock(String msg) {
        ExecutorService executorService = Executors.newFixedThreadPool(6);
        Runnable task = new Runnable() {
            public void run() {
                try {
                    String result = lockService.getValue("sleep");
                    System.err.println("线程:[" + Thread.currentThread().getName() + "]拿到结果=》" + result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        executorService.submit(task);
        executorService.submit(task);
        executorService.submit(task);
        executorService.submit(task);
        executorService.submit(task);
        executorService.submit(task);
    }
}
